#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define LEDDEV_CNT 1
#define LEDDEV_NAME "led_dev"
#define LED_ON_VALUE 1
#define LED_OFF_VALUE 0



struct ledDev_dev
{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    struct device_node *node;
    int gpio_led; /* gpio号 */
};

static struct ledDev_dev ledDev;

void led_switch(int sta)
{

    if (sta == LED_ON_VALUE)
    {
        gpio_set_value(ledDev.gpio_led, LED_ON_VALUE);
    }
    else if (sta == LED_OFF_VALUE)
    {
        gpio_set_value(ledDev.gpio_led, LED_OFF_VALUE);
    }
}

static int led_gpio_init(struct device_node *nd)
{
    int ret = 0;
    ledDev.gpio_led = of_get_named_gpio(nd, "led-gpio", 0);
    if (!gpio_is_valid(ledDev.gpio_led))
    {
        printk(KERN_ERR "led gpio%d is invalid!\r\n", ledDev.gpio_led);
        return -EINVAL;
    }
    ret = gpio_request(ledDev.gpio_led, "LED");
    if (ret)
    {
        printk(KERN_ERR "gpio_request %d failed!\r\n", ledDev.gpio_led);
        return ret;
    }
    gpio_direction_output(ledDev.gpio_led, LED_OFF_VALUE);
    return 0;
}

static int led_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static ssize_t led_read(struct file *filp,
                        char __user *buf,
                        size_t cnt,
                        loff_t *offt)
{
    return 0;
}

/*
 * @description : 向设备写数据
 * @param - filp : 设备文件，表示打开的文件描述符
 * @param - buf : 要写给设备写入的数据
 * @param - cnt : 要写入的数据长度
 * @param - offt : 相对于文件首地址的偏移
 * @return : 写入的字节数，如果为负值，表示写入失败
 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[1];
    unsigned char ledstat;

    retvalue = copy_from_user(databuf, buf, cnt);
    if (retvalue < 0)
    {
        printk(KERN_INFO "kernel write failed!\r\n");
        return -EFAULT;
    }

    ledstat = databuf[0]; /* 获取状态值 */

    if (ledstat == LED_ON_VALUE)
    {
        led_switch(LED_ON_VALUE); /* 打开 LED 灯 */
    }
    else if (ledstat == LED_OFF_VALUE)
    {
        led_switch(LED_OFF_VALUE); /* 关闭 LED 灯 */
    }
    return 0;
}

static struct file_operations led_fops =
    {
        .owner = THIS_MODULE,
        .open = led_open,
        .read = led_read,
        .write = led_write,
};

static int led_prove(struct platform_device *pdev)
{
    int ret = 0;
    printk("led driver and device has matched!\r\n");
    // 1. 初始化 GPIO
    ret = led_gpio_init(pdev->dev.of_node);
    if (ret < 0)
    {
        printk(KERN_ERR "led_gpio_init failed!\r\n");
        return ret;
    }
    // 2. 申请设备号
    ret = alloc_chrdev_region(&ledDev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
    if (ret < 0)
    {
        printk(KERN_ERR "alloc_chrdev_region failed!\r\n");
        goto err_gpio;
    }

    // 3. 初始化 cdev
    ledDev.cdev.owner = THIS_MODULE;
    cdev_init(&ledDev.cdev, &led_fops);
    // 4. 添加 cdev
    ret = cdev_add(&ledDev.cdev, ledDev.devid, LEDDEV_CNT);
    if (ret < 0)
    {
        printk(KERN_ERR "cdev_add failed!\r\n");
        goto err_unregister;
    }
    // 5. 创建类
    ledDev.class = class_create(THIS_MODULE, "led_class");
    if (IS_ERR(ledDev.class))
    {
        printk(KERN_ERR "class_create failed!\r\n");
        goto err_cdev;
    }

    // 6. 创建设备
    ledDev.device = device_create(ledDev.class, NULL, ledDev.devid, NULL, LEDDEV_NAME);
    if (IS_ERR(ledDev.device))
    {
        printk(KERN_ERR "device_create failed!\r\n");
        goto err_class;
    }
    return 0;
err_class:
    class_destroy(ledDev.class);
err_cdev:
    cdev_del(&ledDev.cdev);
err_unregister:
    unregister_chrdev_region(ledDev.devid, LEDDEV_CNT);
err_gpio:
    gpio_free(ledDev.gpio_led);
    return -EIO;
}

static int led_remove(struct platform_device *pdev)
{
    gpio_set_value(ledDev.gpio_led, 0);                 // 关闭LED
    gpio_free(ledDev.gpio_led);                         // 释放 GPIO
    cdev_del(&ledDev.cdev);                             // 删除 cdev
    unregister_chrdev_region(ledDev.devid, LEDDEV_CNT); // 删除设备号
    device_destroy(ledDev.class, ledDev.devid);         // 删除设备
    class_destroy(ledDev.class);                        // 删除类
    printk("led remove!\r\n");
    return 0;
}

// 匹配表
static const struct of_device_id led_of_match[] ={
        {.compatible = "alientek,led"},
        {},
};
// of 匹配
MODULE_DEVICE_TABLE(of, led_of_match);

static struct platform_driver led_driver =
    {
        .probe = led_prove,
        .remove = led_remove,
        .driver =
            {
                .name = "jack-led",// 驱动名字
                .of_match_table = led_of_match, // 设备树匹配
            },
};

static int __init led_init(void)
{
    return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("JACK");
MODULE_INFO(intree, "Y");
